//! @Author: DengLibin 
//! @Date:  2022-04-21 10:04:54
//! @Description:

use std::collections::HashMap;

use actix_web::{delete, get, http::header::ContentType, post, web, Error, HttpResponse, Scope};

use dao::entity::Part;
/// @Author: DengLibin 
/// @Date:  2022-04-21 11:33:27
/// @Description: 路由配置
pub fn route() -> Scope {
    let prodcuts_scope = web::scope("parts");

    prodcuts_scope
        .service(get_parts)
        .service(add_part)
        .service(get_part_detail)
        .service(remove_part)
}
/// @Author: DengLibin 
/// @Date:  2022-04-21 11:50:35
/// @Description: 查询 参数 获取
#[get("")]
pub async fn get_parts(query: web::Query<HashMap<String, String>>) -> Result<HttpResponse, Error> {
    let mut buf = String::new();
    for (k, v) in query.into_inner() {
        buf.push_str(k.as_str());
        buf.push_str("=");
        buf.push_str(v.as_str());
        buf.push(' ');
    }
    Ok(HttpResponse::Ok()
        .content_type(ContentType::plaintext())
        .body(format!("query:{}", buf)))
}
#[post("")]
pub async fn add_part(_new_part: web::Json<Part>) -> Result<HttpResponse, Error> {
    Ok(HttpResponse::Ok().finish())
}
#[get("/{part_id}")]
pub async fn get_part_detail(id: web::Path<String>) -> Result<HttpResponse, Error> {
    Ok(HttpResponse::Ok()
        .content_type(ContentType::plaintext())
        .body(format!("id:{}", id)))
}
#[delete("/{part_id}")]
pub async fn remove_part(id: web::Path<String>) -> Result<HttpResponse, Error> {
    Ok(HttpResponse::Ok()
        .content_type(ContentType::plaintext())
        .body(format!("id:{}", id)))
}
